package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.IZnUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;


@RestController
@RequestMapping("/znUsers")
public class ZnUsersController {
    @Autowired
    private IZnUsersService znUsersService;
    //查询所有用户数据
    @GetMapping("/UserAll")
    public Result UserAll(@RequestParam("pageId") Integer pageId,
                                @RequestParam("pageSize") Integer pageNum,
                                @RequestParam("input") Object Search){
        Page<ZnUsers> znUsersPage = new Page<>(pageId,pageNum);
        LambdaQueryWrapper<ZnUsers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ZnUsers::getUserName,Search)
                .or().like(ZnUsers::getNickname,Search)
                .or().like(ZnUsers::getPhone,Search)
                .or().like(ZnUsers::getEmail,Search);
        Page<ZnUsers> znUsers = znUsersService.page(znUsersPage,queryWrapper);
        return new Result<>(true,"查询成功",200,znUsers);
    }

    //添加用户
    @PostMapping("/addUser")
    public Result<ZnUsers> addUser(@RequestBody ZnUsers znUsersInfo){
        if (znUsersInfo!=null){
            znUsersInfo.setCreateTime(LocalDateTime.now());
            znUsersService.save(znUsersInfo);
            return new Result<>(true,"添加成功",200);
        }else {
            return new Result<>(true, "添加失败，请查看控制台", 400);
        }
    }
    //编辑用户
    @PutMapping("/editorUser")
    public Result<ZnUsers> EditorUser(@RequestBody ZnUsers znUsers){
        znUsers.setUpdateTime(LocalDateTime.now());
        znUsersService.updateById(znUsers);
        return new Result<>(true,"编辑成功",200);
    }
    //删除用户
    @DeleteMapping("/deleteUser/{id}")
    public Result<ZnUsers> deleteUser(@PathVariable Integer id){
        System.out.println("11111111"+id);
        znUsersService.removeById(id);
        return new Result<>(true,"删除成功",200);
    }

    /**
     * 当前登录用户查看自身信息
     * @param username
     * @return
     */
    @GetMapping("/queryUserName")
    public Result<ZnUsers> queryUserName(@RequestParam("userName") String username){
        LambdaQueryWrapper<ZnUsers> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnUsers::getUserName,username);
        ZnUsers znUsers = znUsersService.getOne(wrapper);
        return new Result<>(true,"查询成功",200,znUsers);
    }

    /**
     * 修改个人信息
     * @param znUsers
     * @return
     */
    @PutMapping("/editInformation")
    public Result<ZnUsers> editInformation(@RequestBody ZnUsers znUsers){
        znUsersService.updateById(znUsers);
        return new Result<>(true,"修改成功",200);
    }

    /**
     * 用户修改密码
     * @param passwords
     * @return
     */
    @PutMapping("/changePassword")
    public Result<ZnUsers> changePassword(@RequestBody Map<String, String> passwords){
        String userName = passwords.get("username");
        String oldPassword = passwords.get("oldPassword");
        String confirmPassword = passwords.get("confirmPassword");
        LambdaQueryWrapper<ZnUsers> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnUsers::getUserName,userName);
        ZnUsers znUsers = znUsersService.getOne(wrapper);
        if (znUsers.getPassword().equals(oldPassword)){
            znUsers.setPassword(confirmPassword);
            znUsersService.updateById(znUsers);
            return new Result<>(true,"修改成功",200);
        }else {
            return new Result<>(false,"请检查旧密码是否填写错误",400);
        }
    }

    /**
     * 管理员禁用普通用户
     * @return
     */
    @PostMapping("/toDisable")
    public Result toDisable(@RequestBody ZnUsers znUsers){
        znUsers.setStatus(znUsers.getStatus());
        znUsersService.updateById(znUsers);
        return new Result(true,"成功",200);
    }

    /**
     * 管理员重置普通用户密码
     * @return
     */
    @PostMapping("/resetPasswords")
    public Result resetPasswords(@RequestBody ZnUsers znUsers){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        znUsers.setPassword(encoder.encode(znUsers.getPassword()));
        znUsersService.updateById(znUsers);
        return new Result(true,"成功",200);
    }
}